//
//  RTRetweetStatusView.swift
//  Weibo
//
//  Created by Brisinga on 16/3/9.
//  Copyright © 2016年 Root. All rights reserved.
//

import UIKit
import SnapKit

class RTRetweetStatusView: UIView {
    
    
    private var bottomConstaint: Constraint?
    
    
    // 设置statusViewModel
     var statusViewModel: RTStatusViewModel? {
    
        didSet {
            
            // 设置label的显示内容
            textLabel.text = statusViewModel?.retweetedText
            
            // 这只配图
            self.bottomConstaint?.uninstall()
            
            if let url = statusViewModel?.status?.retweeted_status?.pic_urls where url.count > 0 {
            
                picView.picUrls = url
                
                picView.hidden = false
                
                self.snp_makeConstraints(closure: { (make) -> Void in
                    
                    self.bottomConstaint = make.bottom.equalTo(picView).offset(kMargin).constraint

                })
            
            }else {
            
                picView.hidden = true
                
                self.snp_makeConstraints(closure: { (make) -> Void in
                    
                    self.bottomConstaint = make.bottom.equalTo(textLabel).offset(kMargin).constraint
                })
            }
        }
    }
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    
    
    // 加载内部控件
    private func setupUI() {
    
        backgroundColor = UIColor(white: 239/255, alpha: 1)
        
        // 添加textLabel
        addSubview(textLabel)
        
        // 添加pictureView
        addSubview(picView)
        
        
        // 布局
        textLabel.snp_makeConstraints { (make) -> Void in
            
            make.leading.top.equalTo(self).offset(kMargin)
            make.trailing.equalTo(self).offset(-kMargin)
        }
        
        
        picView.snp_makeConstraints { (make) -> Void in
            
            make.leading.equalTo(textLabel)
            make.top.equalTo(textLabel.snp_bottom).offset(kMargin)
        }
        
        self.snp_makeConstraints { (make) -> Void in
            
            self.bottomConstaint = make.bottom.equalTo(picView).offset(kMargin).constraint
        }
    }

    // 懒加载textLabel
    private lazy var textLabel: UILabel = {
        
        let textLabel = UILabel(textColor: UIColor.darkGrayColor(), textFont: 15)
        
        textLabel.numberOfLines = 0
        
        textLabel.text = "hahahah"
        
        return textLabel
    }()
    
    
    // 懒加载pictureView
    private lazy var picView: RTStatusPickerView = RTStatusPickerView(frame: CGRectZero, collectionViewLayout: UICollectionViewFlowLayout())

}
